// H5 Stripe 集成工具
// 注意：需要后端提供 /addons/drama/pay/prepay 接口，返回 { id } Checkout Session Id

let stripeInstance = null

const loadStripeJs = () => new Promise((resolve, reject) => {
	if (typeof window === 'undefined') return resolve()
	if (window.Stripe) return resolve()
	const script = document.createElement('script')
	script.src = 'https://js.stripe.com/v3/'
	script.onload = () => resolve()
	script.onerror = reject
	document.head.appendChild(script)
})

export async function initStripe(publishableKey) {
	await loadStripeJs()
	if (!window.Stripe) throw new Error('Stripe.js 加载失败')
	stripeInstance = window.Stripe(publishableKey)
	return stripeInstance
}

export function getStripe() {
	return stripeInstance
}

// 跳转到 Checkout
export async function redirectToCheckout(sessionId) {
	if (!stripeInstance) throw new Error('Stripe 未初始化')
	return await stripeInstance.redirectToCheckout({ sessionId })
}



